import { Directive, Input, OnInit, OnDestroy, ElementRef } from '@angular/core';

import { Router } from '@angular/router';

import { AppSettings } from '../app.settings';

import { Util } from '../utils/util';

declare var $: any;

@Directive({
  selector: '[goDetail]'
})

export class GoDetailDirective implements OnInit {


  @Input('data') data: Object;
  @Input('type') type: string;

  private show: boolean = true;

  private info: string = "";

  private user: any;

  constructor(private el: ElementRef, private appSettings: AppSettings, private util: Util, private router: Router) {

  }


  ngOnInit() {

    // console.log(this.data);

    // console.log(this.type);


    // if (this.isExsit(this.type)) {
    //   this.el.nativeElement.hidden = true;
    //   // console.log('hide this dir');
    //   return;
    // } else {
    //   this.el.nativeElement.hidden = false;

    // }
    switch (this.type) {

      // 品牌详情
      case 'brandDetail':

        this.info = "点击查看品牌详情";
        this.bindEvent('brandName', '品牌详情', ['brand', this.data['brandName']]);
        break;

      // 店铺详情
      case 'shopDetail':
        this.info = "点击查看店铺详情";
        this.bindEvent('uid', '店铺详情', ['shop', this.data['uid']]);
        break;

      // 商品详情
      case 'productDetail':
        this.info = "点击查看商品详情";
        this.bindEvent('numberId', '商品详情', ['product', this.data['numberId']]);
        break;

      // 品牌详情
      case 'monitorBrandDetail':

        this.info = "点击查看品牌详情";
        this.bindEvent('brandName', '品牌详情', ['monitorBrand', this.data['brandName']]);
        break;

      // 店铺详情
      case 'monitorShopDetail':
        this.info = "点击查看店铺详情";
        this.bindEvent('uid', '店铺详情', ['monitorShop', this.data['uid']]);
        break;

      // 商品详情
      case 'monitorProductDetail':
        this.info = "点击查看商品详情";
        this.bindEvent('numberId', '商品详情', ['monitorProduct', this.data['numberId']]);
        break;

      case 'liveDetail':
        this.info = "点击查看达人详情";
        this.bindEvent('darenId', '详情', ['live', this.data['darenId']]);
        break;

      default:
        break;
    }


  }


  ngOnDestroy() {
    $(this.el.nativeElement).off('click');
  }

  // 是否已存在着
  isExsit(_state) {

    // let d:any = this.appSettings.navBread.get();

    // let bool:boolean = false;

    // for (let i = d.length - 1; i >= 0; i--) {
    //   if (d[i].state == _state) {

    //     // console.log(d[i].state);

    //     bool = true;
    //     break;
    //   }
    // }

    // console.log(bool);

    // return bool;

  }

  /**
  * [bindEvent description] brandName uid numberId
  * @param  {[type]} _type [description]
  * @return {[type]}       [description]
  */
  bindEvent(key, _navName, _route) {
    $(this.el.nativeElement).on('click', () => {

      console.log(_route);

      let _type:string;

      // type装换
      if(this.type == 'monitorProductDetail'){

         _type = 'productDetail';

      }else if(this.type == 'monitorShopDetail'){

         _type = 'shopDetail';

      }else if(this.type == 'monitorBrandDetail'){

        _type = 'brandDetail';

      }else {
        _type = this.type;
      }

      // 保存
      this.util.sessionStore().set(_type, this.data);

      this.router.navigate(_route);

      //this.appSettings.navBread.push({ 'name': _navName, 'route': _route });


    });
  }
}
